package com.qf.service.impl;

import com.qf.mapper.UserMapper;
import com.qf.pojo.User;
import com.qf.service.UserService;

public class UserServiceImpl implements UserService {
    UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public void transfer(String from, String to, int money) {
        //查询两个账户
        User fromUser = userMapper.findByName(from);
        User toUser = userMapper.findByName(to);

        //判断源 账户余额 - money 是否大于 0
        if(fromUser.getMoney() - money < 0) {
            System.out.println("刘润达哭了");
            return;
        }
        //转账
        fromUser.setMoney(fromUser.getMoney() - money);
        toUser.setMoney(toUser.getMoney() + money);
        //更新数据库
        userMapper.update(fromUser);
        System.out.println(1/0);
        userMapper.update(toUser);
    }
}
